﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_IntroDAL : IDcms_IntroDAL
    {
        public Dcms_IntroModel AddModel(Dcms_IntroModel model)
        {
            string sQLString = "insert into Dcms_Intro(Intro_CateId,Intro_Title,Intro_State,Intro_Image,Intro_Content,Intro_Order,Intro_AddTime,Intro_ExFlag1,Intro_ExFlag2,Intro_ExFlag3,Intro_ExFlag4,Intro_ExFlag5,Intro_ExFlag6,Intro_ExFlag7,Intro_ExFlag8,Intro_ExFlag9,Intro_ExFlag10) values(@Intro_CateId,@Intro_Title,@Intro_State,@Intro_Image,@Intro_Content,@Intro_Order,@Intro_AddTime,@Intro_ExFlag1,@Intro_ExFlag2,@Intro_ExFlag3,@Intro_ExFlag4,@Intro_ExFlag5,@Intro_ExFlag6,@Intro_ExFlag7,@Intro_ExFlag8,@Intro_ExFlag9,@Intro_ExFlag10)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { 
                new OleDbParameter("@Intro_CateId", OleDbType.Integer), new OleDbParameter("@Intro_Title", OleDbType.VarChar), new OleDbParameter("@Intro_State", OleDbType.VarChar), new OleDbParameter("@Intro_Image", OleDbType.VarChar), new OleDbParameter("@Intro_Content", OleDbType.LongVarChar), new OleDbParameter("@Intro_Order", OleDbType.Integer), new OleDbParameter("@Intro_AddTime", OleDbType.DBTimeStamp), new OleDbParameter("@Intro_ExFlag1", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag2", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag3", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag4", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag5", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag6", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag7", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag8", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag9", OleDbType.VarChar), 
                new OleDbParameter("@Intro_ExFlag10", OleDbType.VarChar)
             };
            cmdParms[0].Value = model.Intro_CateId;
            cmdParms[1].Value = model.Intro_Title;
            cmdParms[2].Value = model.Intro_State;
            cmdParms[3].Value = model.Intro_Image;
            cmdParms[4].Value = model.Intro_Content;
            cmdParms[5].Value = model.Intro_Order;
            cmdParms[6].Value = model.Intro_AddTime;
            cmdParms[7].Value = model.Intro_ExFlag1;
            cmdParms[8].Value = model.Intro_ExFlag2;
            cmdParms[9].Value = model.Intro_ExFlag3;
            cmdParms[10].Value = model.Intro_ExFlag4;
            cmdParms[11].Value = model.Intro_ExFlag5;
            cmdParms[12].Value = model.Intro_ExFlag6;
            cmdParms[13].Value = model.Intro_ExFlag7;
            cmdParms[14].Value = model.Intro_ExFlag8;
            cmdParms[15].Value = model.Intro_ExFlag9;
            cmdParms[0x10].Value = model.Intro_ExFlag10;
            model.Intro_Id = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool CopyIntro(int SourceCateID, int DestCateID)
        {
            string sQLString = "insert into Dcms_Intro (Intro_CateId, Intro_Title, Intro_State, Intro_Image, Intro_Content, Intro_Order, Intro_AddTime, Intro_ExFlag1, Intro_ExFlag2, Intro_ExFlag3, Intro_ExFlag4, Intro_ExFlag5, Intro_ExFlag6, Intro_ExFlag7, Intro_ExFlag8, Intro_ExFlag9, Intro_ExFlag10)";
            sQLString = sQLString + "select @DestCateID, Intro_Title, Intro_State, Intro_Image, Intro_Content, Intro_Order, Intro_AddTime, Intro_ExFlag1, Intro_ExFlag2, Intro_ExFlag3, Intro_ExFlag4, Intro_ExFlag5, Intro_ExFlag6, Intro_ExFlag7, Intro_ExFlag8, Intro_ExFlag9, Intro_ExFlag10 from Dcms_Intro where Intro_CateId=@SourceCateID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@SourceCateID", OleDbType.Integer), new OleDbParameter("@DestCateID", OleDbType.Integer) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return OleDbHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public IList<Dcms_IntroModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_IntroModel> list = new List<Dcms_IntroModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_IntroModel item = new Dcms_IntroModel();
                if (ds.Tables[0].Rows[i]["Intro_Id"].ToString() != "") item.Intro_Id = int.Parse(ds.Tables[0].Rows[i]["Intro_Id"].ToString());
                if (ds.Tables[0].Rows[i]["Intro_CateId"].ToString() != "") item.Intro_CateId = int.Parse(ds.Tables[0].Rows[i]["Intro_CateId"].ToString());
                item.Intro_Title = ds.Tables[0].Rows[i]["Intro_Title"].ToString();
                item.Intro_State = ds.Tables[0].Rows[i]["Intro_State"].ToString();
                item.Intro_Image = ds.Tables[0].Rows[i]["Intro_Image"].ToString();
                item.Intro_Content = ds.Tables[0].Rows[i]["Intro_Content"].ToString();
                if (ds.Tables[0].Rows[i]["Intro_Order"].ToString() != "") item.Intro_Order = int.Parse(ds.Tables[0].Rows[i]["Intro_Order"].ToString());
                if (ds.Tables[0].Rows[i]["Intro_AddTime"].ToString() != "") item.Intro_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Intro_AddTime"].ToString());
                item.Intro_ExFlag1 = ds.Tables[0].Rows[i]["Intro_ExFlag1"].ToString();
                item.Intro_ExFlag2 = ds.Tables[0].Rows[i]["Intro_ExFlag2"].ToString();
                item.Intro_ExFlag3 = ds.Tables[0].Rows[i]["Intro_ExFlag3"].ToString();
                item.Intro_ExFlag4 = ds.Tables[0].Rows[i]["Intro_ExFlag4"].ToString();
                item.Intro_ExFlag5 = ds.Tables[0].Rows[i]["Intro_ExFlag5"].ToString();
                item.Intro_ExFlag6 = ds.Tables[0].Rows[i]["Intro_ExFlag6"].ToString();
                item.Intro_ExFlag7 = ds.Tables[0].Rows[i]["Intro_ExFlag7"].ToString();
                item.Intro_ExFlag8 = ds.Tables[0].Rows[i]["Intro_ExFlag8"].ToString();
                item.Intro_ExFlag9 = ds.Tables[0].Rows[i]["Intro_ExFlag9"].ToString();
                item.Intro_ExFlag10 = ds.Tables[0].Rows[i]["Intro_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return OleDbHelper.ExecuteSql("delete from Dcms_Intro where Intro_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Intro where Intro_Id=@Intro_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Intro_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return OleDbHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_IntroModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(OleDbHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_IntroModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Intro where Intro_Id=@Intro_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Intro_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            Dcms_IntroModel model = new Dcms_IntroModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_IntroModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Intro";
            return OleDbHelper.CreateDataSet(sql);
        }

        public Dcms_IntroModel GetNewModelByCatID(int CatID)
        {
            string sQLString = " select top 1 * from Dcms_Intro where Intro_CateId=@Intro_CateId order by Intro_Id desc ";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Intro_CateId", OleDbType.Integer) };
            cmdParms[0].Value = CatID;
            Dcms_IntroModel model = new Dcms_IntroModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_IntroModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public bool MoveIntro(int SourceCateID, int DestCateID)
        {
            string sQLString = "update Dcms_Intro set Intro_CateId=@DestCateID where Intro_CateId=@SourceCateID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@SourceCateID", OleDbType.Integer), new OleDbParameter("@DestCateID", OleDbType.Integer) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return OleDbHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public Dcms_IntroModel Update(Dcms_IntroModel model)
        {
            string sQLString = "update Dcms_Intro set Intro_CateId=@Intro_CateId,Intro_Title=@Intro_Title,Intro_State=@Intro_State,Intro_Image=@Intro_Image,Intro_Content=@Intro_Content,Intro_Order=@Intro_Order,Intro_AddTime=@Intro_AddTime,Intro_ExFlag1=@Intro_ExFlag1,Intro_ExFlag2=@Intro_ExFlag2,Intro_ExFlag3=@Intro_ExFlag3,Intro_ExFlag4=@Intro_ExFlag4,Intro_ExFlag5=@Intro_ExFlag5,Intro_ExFlag6=@Intro_ExFlag6,Intro_ExFlag7=@Intro_ExFlag7,Intro_ExFlag8=@Intro_ExFlag8,Intro_ExFlag9=@Intro_ExFlag9,Intro_ExFlag10=@Intro_ExFlag10 where Intro_Id=@Intro_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { 
                new OleDbParameter("@Intro_CateId", OleDbType.Integer), new OleDbParameter("@Intro_Title", OleDbType.VarChar), new OleDbParameter("@Intro_State", OleDbType.VarChar), new OleDbParameter("@Intro_Image", OleDbType.VarChar), new OleDbParameter("@Intro_Content", OleDbType.LongVarChar), new OleDbParameter("@Intro_Order", OleDbType.Integer), new OleDbParameter("@Intro_AddTime", OleDbType.DBTimeStamp), new OleDbParameter("@Intro_ExFlag1", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag2", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag3", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag4", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag5", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag6", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag7", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag8", OleDbType.VarChar), new OleDbParameter("@Intro_ExFlag9", OleDbType.VarChar), 
                new OleDbParameter("@Intro_ExFlag10", OleDbType.VarChar), new OleDbParameter("@Intro_Id", OleDbType.Integer)
             };
            cmdParms[0].Value = model.Intro_CateId;
            cmdParms[1].Value = model.Intro_Title;
            cmdParms[2].Value = model.Intro_State;
            cmdParms[3].Value = model.Intro_Image;
            cmdParms[4].Value = model.Intro_Content;
            cmdParms[5].Value = model.Intro_Order;
            cmdParms[6].Value = model.Intro_AddTime;
            cmdParms[7].Value = model.Intro_ExFlag1;
            cmdParms[8].Value = model.Intro_ExFlag2;
            cmdParms[9].Value = model.Intro_ExFlag3;
            cmdParms[10].Value = model.Intro_ExFlag4;
            cmdParms[11].Value = model.Intro_ExFlag5;
            cmdParms[12].Value = model.Intro_ExFlag6;
            cmdParms[13].Value = model.Intro_ExFlag7;
            cmdParms[14].Value = model.Intro_ExFlag8;
            cmdParms[15].Value = model.Intro_ExFlag9;
            cmdParms[0x10].Value = model.Intro_ExFlag10;
            cmdParms[0x11].Value = model.Intro_Id;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return OleDbHelper.ExecuteSql("Update Dcms_Intro Set Intro_State='" + StateValue.ToString() + "' where Intro_Id In (" + AllID + ")", null) > 0;
        }
    }
}

